/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compiler;

import interfaz.Interfaz;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Stack;

import walle.compi.classe.node.*;

/**
 *
 * @author 
 */
public class Helper {
    
    private static double temp_int;
    private static String temp_string;
    private static String print_selector;
	private static String operator;
    private static boolean flag;
	private static boolean flag_select;
    private static boolean flag_boolean;
    private static String logic;
   
    public static Stack<String> stack1;
    public static Stack<String> stack2;
    
	public static Hashtable<String, Integer> variables = new Hashtable<String, Integer>();
	public static Hashtable<String, AProdTo> function = new Hashtable<String, AProdTo>();

	
	public static boolean isFlag_boolean() {
		return flag_boolean;
	}

	public static void setFlag_boolean(boolean flag_boolean) {
		Helper.flag_boolean = flag_boolean;
	}

	
	 public static String getLogic() {
		return logic;
	}

	public static void setLogic(String logic) {
		Helper.logic = logic;
	}

	public static boolean isFlag_select() {
			return flag_select;
		}

		public static void setFlag_select(boolean flag_select) {
			Helper.flag_select = flag_select;
		}
	
	public static String getPrint_selector() {
		return print_selector;
	}

	public static void setPrint_selector(String print_selector) {
		Helper.print_selector = print_selector;
	}
    
    public static String getTemp_string() {
		return temp_string;
	}

	public static void setTemp_string(String temp_string) {
		Helper.temp_string = temp_string;
	}

	public static boolean isFlag() {
		return flag;
	}

	public static void setFlag(boolean flag) {
		Helper.flag = flag;
	}


    public static double getTemp_int() {
        return temp_int;
    }


    public static void setTemp_int(double aTemp_int) {
        temp_int = aTemp_int;
    }


    public static String getOperator() {
        return operator;
    }


    public static void setOperator(String aOperator) {
        operator = aOperator;
    }
    
    public static void fileWriter(String s){
    	try {
    		StringBuilder contents = new StringBuilder();
    		BufferedReader reader = new BufferedReader(new FileReader(Interfaz.rutArchivo),1024);
    		String line = null;
            while (( line = reader.readLine()) != null){
            	contents.append(line);
            	contents.append(System.getProperty("line.separator"));
              }
    		
    		PrintWriter escribir = new PrintWriter(new BufferedWriter(new FileWriter(Interfaz.rutArchivo)));
			escribir.write(contents.toString()+s+"\r\n");
			
			escribir.close();
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public static void historyWriter(String s){
    	Interfaz.history.setText(Interfaz.history.getText()+s+"\r\n");
    }
    
    public static void compare(Double a, Double b){
    	if (logic == "simple"){
    		if (a.equals(b)){
    			flag_boolean = true;
    		}else{
    			flag_boolean = false;
    		}
    	}else if (logic == "and"){
    		if (!a.equals(b)){
    			flag_boolean = false;
    		}
    	}else if (logic == "or"){
    		if (a.equals(b)){
    			flag_boolean = true;
    		}
    	}
    }
    
   public static void concatenate(String s){
	   if (print_selector == "iden"){
		   if (flag == true){
		   		setTemp_string(s);
   			flag = false;
   			}else{
   				setTemp_string(temp_string+" "+s);
   			}
	   }else if(print_selector == "first"){
		   if (flag == true){
		   		setTemp_string(s);
		   		flag = false;
  			}
	   }else if(print_selector == "butfirst"){
		   if (flag == true){
		   		flag = false;
		   		flag_select = true;
 			}else if (flag_select == true){
   				setTemp_string(s);
   				flag_select = false;
 			}else{
 				setTemp_string(temp_string+" "+s);
 			}
   		}else if(print_selector == "last"){
		   		setTemp_string(s);
  		}else if(print_selector == "word"){
  			setTemp_string(temp_string+s);
		}
   }
   
    
    
    public static void solveMath(double a){
    	if(operator.equals("sum")){
            setTemp_int(getTemp_int() + a);
        }
        else if (operator.equals("diff")){
        	
        	if (flag == true){
        		setTemp_int(a);
        		flag = false;
        	}else{
        	setTemp_int(getTemp_int() - a);
        	}
        }
        else if (operator.equals("product")){

        	if (flag == true){
        		setTemp_int(a);
        		flag = false;
        	}else{
        	setTemp_int(getTemp_int() * a);
        	}
        }	
        else if (operator.equals("power")){

        	if (flag == true){
        		setTemp_int(a);
        		flag = false;
        	}else{
        	setTemp_int(Math.pow(getTemp_int(), a));
        	}
        }
        else if (operator.equals("round")){
            	setTemp_int(Math.rint(a));
        } 	
        else if (operator.equals("sqrt")){
        	setTemp_int(Math.sqrt(a));
        }
    	else if (operator.equals("exp")){
        	setTemp_int(Math.exp(a));
        }
    	else if (operator.equals("exp")){
        	setTemp_int((int)(a));
        }
    	else if (operator.equals("min")){
        	setTemp_int(-a);
        }
        else if (operator.equals("div")){

        	if (flag == true){
        		setTemp_int(a);
        		flag = false;
        	}else{
        	setTemp_int(getTemp_int() / a);
        	}
        } 
        else if (operator.equals("modulo")){

        	if (flag == true){
        		setTemp_int(a);
        		flag = false;
        	}else{
        	setTemp_int(getTemp_int() % a);
        	}
        } 
    }
}
